// pages/chat/chat.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contacts: [],
    // 触摸开始时间
    touchStartTime: 0,
    // 触摸结束时间
    touchEndTime: 0,
    // 最后一次单击事件点击发生时间
    lastTapTime: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    this.updateMessageList();

    app.registerGlobalMessageReceiver(function(msg){
      that.updateMessageList();
    });
  },

  updateMessageList: function(){
    var contacts = [];
    for(var id in app.globalData.history){
      var user = app.globalData.history[id]["user"];
      var length = app.globalData.history[id]["message"].length
      if(length == 0){
        contacts.push({
          "id": id,
          "nickname": user["nick_name"],
          "avatar": user["profile_pic"],
          "new": app.globalData.history[id]["new"]
        });
        continue;
      }
      var record = app.globalData.history[id]["message"][length - 1];
      var date = new Date(record["time"]);
      var degist = "";
      if(record["type"] === "text"){
        degist = record["text"].substring(0, 12) + (record["text"].length > 12 ? "..." : "");
      } else if (record["type"] === "image"){
        degist = "[图片消息]"
      } else if (record["type"] === "book"){
        degist = "[书本信息]"
      } else {
        degist = "[未知信息]"
      }
      contacts.push({
        "id": id,
        "nickname": user["nick_name"],
        "avatar": user["profile_pic"],
        "record": degist,
        "time": (date.getMonth() + 1) + "-" + (date.getDate()) + " " + (date.getHours()) + ":" + (date.getMinutes()),
        "timestamp": record["time"],
        "new": app.globalData.history[id]["new"]
      })
    }
    contacts = contacts.sort(function (a, b) { return a["timestamp"] < b["timestamp"]})
    console.log(contacts);
    this.setData({
      contacts: contacts
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.refreshUserInfo(function () {
      that.updateMessageList();
    })
  },

  onItemLongTap: function(event){
    var that = this;
    var id = event.currentTarget.dataset["id"];
    console.log(id);
    wx.showModal({
      title: '删除记录',
      content: '你确定要删除这条消息记录吗',
      success: function(e){
        if(e.cancel) return;
        delete app.globalData.history[id];
        that.updateMessageList();
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.unregisterGlobalMessageReceiver();
  },

  /**
   * 点击条目时
   */
  onItemTap: function(event){
    var that = this;
    if (that.touchEndTime - that.touchStartTime < 350) {
      // 当前点击的时间
      var currentTime = event.timeStamp
      var lastTapTime = that.lastTapTime
      // 更新最后一次点击时间
      that.lastTapTime = currentTime

      var id = event.currentTarget.dataset["id"];
      var nickname = event.currentTarget.dataset["nickname"];
      app.globalData.history[id]["new"] = false;

      wx.navigateTo({
        url: 'interaction/interaction?id=' + id + "&nickname=" + nickname
      });
    }
  },

  /// 按钮触摸开始触发的事件
  touchStart: function (e) {
    this.touchStartTime = e.timeStamp
  },

  /// 按钮触摸结束触发的事件
  touchEnd: function (e) {
    this.touchEndTime = e.timeStamp
  }
})